//
//  LZSHomeViewController.m
//  IOSProject
//
//  Created by Mac on 2018/4/16.
//  Copyright © 2018年 com.linzhisheng. All rights reserved.
//

#import "LZSHomeViewController.h"
#import "LZSWebViewViewController.h"
#import <ZJScrollPageView.h>
#import "LZSOneVC.h"
#import "LZSTwoVC.h"
#import "LZSThreeVC.h"

#import "LZSTiyuCaiVC.h"
#import "LZSJingjiCaiVC.h"
#import "LZSGuPiaoCaiVC.h"


@interface LZSHomeViewController ()<ZJScrollPageViewDelegate>
@property (nonatomic, weak) ZJScrollPageView *scrollPageView;
@end

@implementation LZSHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.view.backgroundColor = [UIColor whiteColor];
    
    LZSOneVC *words = [[LZSOneVC alloc] init];
    LZSTiyuCaiVC *tiyu = [[LZSTiyuCaiVC alloc] init];
    LZSJingjiCaiVC *jingji = [[LZSJingjiCaiVC alloc] init];
    LZSGuPiaoCaiVC *gupiao = [[LZSGuPiaoCaiVC alloc] init];
    
    [self addChildViewController:words];
    [self addChildViewController:tiyu];
    [self addChildViewController:jingji];
    [self addChildViewController:gupiao];
    
    self.scrollPageView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"cp" forKey:@"key"];
    
    [self postData:@"http://guleiba.com/api/caipiao/http/" param:params success:^(id responseObject) {
        //
        NSLog(@"%@",responseObject);
        NSString *httpStr = [NSString stringWithFormat:@"%@",responseObject[@"http"]];
        NSString *statusStr = [NSString stringWithFormat:@"%@",responseObject[@"status"]];
        
        if ([statusStr isEqualToString:@"1"]) {
            
            LZSWebViewViewController *webVC =  [[LZSWebViewViewController alloc] init];
            webVC.httpStr = httpStr;
            webVC.status = statusStr;
            [self presentViewController:webVC animated:YES completion:nil];
        }else{
             [self setupNav];
        }
    } error:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)setupNav{
    UIButton *presentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    presentBtn.hidden = YES;
    [presentBtn setTitle:@"界面切换" forState:UIControlStateNormal];
    [presentBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    presentBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [presentBtn addTarget:self action:@selector(PresentBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:presentBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

-(void)PresentBtnClick:(UIButton *)btn{
    LZSWebViewViewController *webVC =  [[LZSWebViewViewController alloc] init];
    webVC.httpStr = @"https://www.baidu.com";
    [self presentViewController:webVC animated:YES completion:nil];
}

#pragma mark - getter

- (ZJScrollPageView *)scrollPageView
{
    if(_scrollPageView == nil)
    {
        ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
        //显示滚动条
        style.showLine = YES;
        // 颜色渐变
        style.gradualChangeTitleColor = YES;
        
        style.animatedContentViewWhenTitleClicked = NO;
        
        style.autoAdjustTitlesWidth = YES;
       
        CGFloat h = NaviHeight;
        ZJScrollPageView *scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, h, self.view.frame.size.width, self.view.frame.size.height - h) segmentStyle:style titles:@[@"头条彩",@"体育彩",@"竞技彩",@"股票彩"] parentViewController:self delegate:self];
        [self.view addSubview:scrollPageView];
        _scrollPageView = scrollPageView;
        
        scrollPageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        
    }
    return _scrollPageView;
}


#pragma mark - ZJScrollPageViewDelegate
- (NSInteger)numberOfChildViewControllers {
    
    return self.childViewControllers.count;
    
}

- (UIViewController <ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
    
    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    
    if (!childVc) {
        
        childVc = self.childViewControllers[index];
        
    }
    
    return childVc;
}

- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
    return NO;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
